package com.example.petcare.service;



import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

import com.example.petcare.config.AlipayConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;


import java.io.IOException;
import java.util.Map;


@Slf4j
@Service
public class PaymentService {
    private static final String PRODUCTCODE = "FAST_INSTANT_TRADE_PAY";// 销售产品码

    @Autowired
    private OrderService orderService;


    // 创建支付宝订单
    public String create(String orderId, String orderName, String payPrice) {
        AlipayClient client = new DefaultAlipayClient(AlipayConfig.URL, AlipayConfig.APPID,
                AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT,
                AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY,
                AlipayConfig.SIGNTYPE);// 获得初始化的AlipayClient,
        AlipayTradePagePayRequest payRequest = new AlipayTradePagePayRequest();// 创建API对应的request
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();// 创建API对应的request
        model.setOutTradeNo(orderId);// 商户订单号
        model.setSubject(orderName);// 订单名称
        model.setTotalAmount(payPrice);// 付款金额
        model.setProductCode(PRODUCTCODE);// 销售产品码
        payRequest.setBizModel(model);// 填充业务参数


        payRequest.setReturnUrl(AlipayConfig.RETURN_URL);// 支付成功后跳转的页面
        payRequest.setNotifyUrl(AlipayConfig.NOTIFY_URL);// 支付成功后通知地址

        try {
            return client.pageExecute(payRequest).getBody();// 调用SDK生成表单
        } catch (AlipayApiException e) {
            log.error("[支付宝] 支付失败", e);
        }
        return null;
    }





}

